//
//  MessageView.swift
//  SwiftUIProject
//
//  Created by ZhouShuai on 2024/9/11.
//

import SwiftUI

// 定义数组，存放数据
var messages: [Message] = [
    Message(name: "这是微信", image: "ic_message_wechat"),
    Message(name: "这是QQ", image: "ic_message_qq"),
    Message(name: "这是手机", image: "ic_message_phone"),
    Message(name: "这是邮箱", image: "ic_message_mail"),
]

struct MessageView: View {
    // 使用"@State"定义数据，让SwiftUI监控属性，并在属性值发生变化时更新UI
    @State var messageItems = messages

    var body: some View {
        NavigationStack {
            List {
                ForEach(messageItems) { message in
                    HStack {
                        Image(message.image)
                            .resizable()
                            .frame(width: 60, height: 60)
                            .cornerRadius(5)
                        Text(message.name)
                            .padding()
                    }
                }.onDelete(perform: { indexSet in
                    onDeleteRow(at: indexSet)
                })
            }
            .listStyle(.plain)
            .navigationTitle("Message")
            .toolbarBackground(.pink, for: .navigationBar)
        }
    }

    // 滑动删除
    func onDeleteRow(at atOffsets: IndexSet) {
        messageItems.remove(atOffsets: atOffsets)
    }

    // 拖动排序方法
    func moveItem(from source: IndexSet, to destination: Int) {
    }
}

/**
 新的导航系统最直接的变化是废弃了 NavigationView，将其功能分成了两个单独的控件:
 1. NavigationStack：针对的是单栏的使用场景，例如 iPhone 、Apple TV、Apple Watch：
 2. NavigationSplitView：则针对的是多栏场景，例如 ：iPadOS 、macOS：

 */
